package com.shujia;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class Code02Admin {

    Connection connection;

    @Before
    public void getConnection(){
        Configuration configuration = new Configuration();

        // 注意：需要在当前平台中配置node1 node2的主机映射
        configuration.set("hbase.zookeeper.quorum","192.168.163.100,node1,node2");
        try {
            connection  = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建表
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {
        Admin admin = connection.getAdmin();
//        new TableDescriptor()

//        TableName name
//        new TableName()

        TableName tableName = TableName.valueOf("api:stu");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        // Table should have at least one column family. 至少给定一个列族信息
        // HColumnDescriptor family
        HColumnDescriptor info = new HColumnDescriptor("info");
        hTableDescriptor.addFamily(info);
        admin.createTable(hTableDescriptor);

    }

    /**
     * 修改表
     * @throws IOException
     */
    @Test
    public void alterTable() throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("api:stu");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        // Table should have at least one column family. 至少给定一个列族信息
        // HColumnDescriptor family
        HColumnDescriptor info = new HColumnDescriptor("info");
        hTableDescriptor.addFamily(info);

        HColumnDescriptor score = new HColumnDescriptor("score");
        HColumnDescriptor compute = new HColumnDescriptor("compute");
        hTableDescriptor.addFamily(score);
        hTableDescriptor.addFamily(compute);
//        admin.createTable(hTableDescriptor);
        admin.modifyTable(hTableDescriptor);
    }

    @Test
    public void alterTable2() throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("api:stu");
        HColumnDescriptor school = new HColumnDescriptor("school");
        admin.addColumnFamily(tableName,school);

        // 获取列族对象方式2(新)
//        ColumnFamilyDescriptor build = ColumnFamilyDescriptorBuilder.newBuilder("test".getBytes()).build();
//        admin.addColumnFamily(tableName,build);
    }


    @After
    public void close(){
        try {
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
